﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Xml.Serialization;
using ViewMaker.Core.Utils;

namespace DbStreamConverter
{
    /// <summary>
    /// App.xaml の相互作用ロジック
    /// </summary>
    public partial class App : Application
    {
        private void Application_Startup_1(object sender, StartupEventArgs e)
        {
            Trace.AutoFlush = true;
            Trace.Listeners.Add(new TextWriterTraceListener("DbStreamConverter.log", "LogFile"));

            if (e.Args.Length >= 1)
            {
                if (e.Args.Length < 3)
                {
                    MessageBox.Show("DbStreamConverter [FileName.mapping] [SourceConnectionString] [TargetConnectionString]");
                    Environment.Exit(-1);
                }
                try
                {
                    using (var file = new FileStream(e.Args[0], FileMode.Open, FileAccess.Read))
                    {
                        var xmlSeri = new XmlSerializer(typeof(MappingVM));
                        var vm = (MappingVM)xmlSeri.Deserialize(file);
                        vm.Init();
                        vm.SourceConnectionString = e.Args[1];
                        vm.TargetConnectionString = e.Args[2];
                        vm.ConvertDb();
                    }
                    Environment.Exit(0);
                }
                catch (Exception ex)
                {
                    Trace.WriteLine(ex.ToString());
                    Environment.Exit(-1);
                }
            }
        }
    }
}
